package com.siwei.mes.service.system.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.entity.system.User;
import com.siwei.mes.mapper.system.UserMapper;
import com.siwei.mes.service.system.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author Administrator
 * @Date 2023/12/5 9:38
 * @Version 1.0
 * @Description 获取登录相关的
 */
@Slf4j
@Service
public class AuthServiceImpl implements AuthService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Long getLoginUserId() {
        boolean login = StpUtil.isLogin();
        if (login) {
            return StpUtil.getLoginIdAsLong();
        }
        return null;
    }

    @Override
    public String getLoginUserName() {
        boolean login = StpUtil.isLogin();
        if (login) {
            Object userName = StpUtil.getExtra("userName");
            if (userName != null) {
                return userName.toString();
            }
        }
        return null;
    }

    @Override
    public String getLoginUserPhone() {
        boolean login = StpUtil.isLogin();
        if (login) {
            Object userPhone = StpUtil.getExtra("userPhone");
            if (userPhone != null) {
                return userPhone.toString();
            }
        }
        return null;
    }

    @Override
    public String getLoginUserRole() {
        boolean login = StpUtil.isLogin();
        if (login) {
            Object roleId = StpUtil.getExtra("roleId");
            if (roleId != null) {
                return roleId.toString();
            }
        }
        return null;
    }

    @Override
    public String getLoginUserProjectJson() {
        boolean login = StpUtil.isLogin();
        if (login) {
            Object testProjectJson = StpUtil.getExtra("testProjectJson");
            if (testProjectJson != null) {
                return testProjectJson.toString();
            }
        }
        return null;
    }

    @Override
    public String getSocietyUserName() {
        boolean login = StpUtil.isLogin();
        if (login) {
            Object societyUserName = StpUtil.getExtra("societyUserName");
            if (societyUserName != null) {
                return societyUserName.toString();
            }
        }
        return null;
    }

    @Override
    public Set<Integer> getLoginUserExperimentType() {
        Set<Integer> experimentTypeList = new HashSet<>();
        User user = userMapper.selectByPrimaryKey(this.getLoginUserId());
        if (user != null) {
            JSONArray testProjectJson = user.getTestProjectJson();
            for (int i = 0; i < testProjectJson.size(); i++) {
                JSONObject jsonObject = (JSONObject) testProjectJson.get(i);
                experimentTypeList.add(jsonObject.getIntValue("no"));
            }
        }
        return experimentTypeList;
    }
}
